package com.ximalaya.demo;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

public class MainActivity extends AppCompatActivity {

    LiveCourseChooseAdapter liveCourseChooseAdapter;
    LiveCourseChooseAdapter.LiveItemHolder holder;


    Handler handler = new Handler();
    int pageSize = 3;

    private Runnable animViewPager = new Runnable(){
        @Override
        public void run() {
            if (holder!=null && holder.t!=null) {
                holder.t.setCurrentItem((holder.t.getCurrentItem()+1) % pageSize, true);
//                liveCourseChooseAdapter.setCurrentIndex();
                handler.postDelayed(animViewPager, 2000L);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        liveCourseChooseAdapter = new LiveCourseChooseAdapter();
        holder = new LiveCourseChooseAdapter.LiveItemHolder(findViewById(R.id.containerRl));

        holder.t.setAdapter(new LiveCourseViewPagerAdapter(this,3));
        holder.s.setUpWidthViewPager(holder.t);
        holder.t.setCanGoLeft(false);
        holder.t.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                liveCourseChooseAdapter.a(holder, pageSize);
                Log.d(MainActivity.class.getSimpleName(),"currentPage:"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager2.SCROLL_STATE_DRAGGING) {
//                            DailyChooseAdapter.c(DailyChooseAdapter.this);
                }
            }
        });

    }



    @Override
    protected void onResume() {
        super.onResume();
//        liveCourseChooseAdapter.a(holder,3);
        liveCourseChooseAdapter.start(holder,this);

        handler.postDelayed(animViewPager, 2000l);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(handler!=null){
            handler.removeCallbacks(animViewPager);
        }
    }
}